<?php
/*------------------------------------------------------------------------
 * 文章管理
 * 	
 * Created on 2014-11-29
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2014 http://www.suncco.com All rights reserved.
 * ------------------------------------------------------------------------
 */
namespace Admin\Controller;

class ArticleController extends CommonController{
	public $exec_comfunc  = 'index,add,edit,delete,del,recycle';//index,add,edit,delete
	
	public function index(){
		$type = $this->_get('type', '对不起，您的操作有误！');
		$this->assign('type', $type);
		// 按类别
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
			$this->assign('info', $where);
		}
		// 按标题
		$keyword = $this->_get('keyword');
		if($keyword){
			$where['title'] = array('like', '%' . $keyword . '%');
		}

		$where['module']    = $type;
		$where['is_delete'] = 0;
		$where['status']    = array('in', '0,1');
		$this->_boolean_field = array('no');
		parent::index('',$where,'order_id,id desc');
	}
	public function recycle(){
		$where['module'] = $this->_get('type');
		parent::recycle('', $where);
	}
	//添加文章
	public function add(){
		$type = $this->_get('type', '对不起，您的操作有误！');
		//表单提交处理
		if(IS_POST){
			$model = D('Article');
			if($data = $model->create()){
				$data['cover_id'] = implode(',', $data['cover_id']);
				if(substr($data['cover_id'], 0, 2) == '0,'){
					$data['cover_id'] = substr($data['cover_id'], 2);
				}
				$data['description'] = str_cut($data['description'], 200, '');
				$data['create_time'] = time();
				$data['edit_time']   = time();
				$data['admin_id']    = $this->_user_id;
				unset($data['content']);

				if(false !== $model->add($data)) {
					unset($data);
					$article_id = $model->getLastInsID();
					$data['article_id'] = $article_id;
					$data['content']    = htmlspecialchars_decode($this->_post('content'));
					D('ArticleContent')->add($data);

					// 标签处理
					$tags_id_arr = $this->_post('tags_id');
					D('Common')->delete_tags('article', 'tags_id', $article_id);
					foreach ($tags_id_arr as $tags_id) {
						D('Common')->add_tags('article', 'tags_id', $article_id, $tags_id);
					}

					$this->success('添加成功！', U('index', array('type' =>  $type)));
				}else{
					$this->error('添加失败！');
				}
			}else{
				$this->error($model->getError());
			}
		}
		$this->display('edit');
	}
	//修改文章
	public function edit(){
		$article_id = $this->_get('id', '对不起，您的操作有误！');
		$type       = $this->_get('type', '对不起，您的操作有误！');
		//表单提交处理
		if(IS_POST){
			$model = D('Article');
			if($data = $model->create()){
				$data['cover_id'] = implode(',', $data['cover_id']);
				if(substr($data['cover_id'], 0, 2) == '0,'){
					$data['cover_id'] = substr($data['cover_id'], 2);
				}
				$data['description'] = str_cut($data['description'], 200, '');
				$data['create_time'] = time();
				$data['edit_time']   = time();
				$data['admin_id']    = $this->_user_id;
				unset($data['content']);

				if(false !== $model->save($data)) {
					$content = htmlspecialchars_decode($this->_post('content'));
					unset($data, $where);
					$where['article_id'] = $article_id;
					if(D('ArticleContent')->where($where)->count()){
						$data['content'] = $content;
						D('ArticleContent')->where($where)->save($data);
					}else{
						$data['content']    = $content;
						$data['article_id'] = $article_id;
						D('ArticleContent')->add($data);
					}

					// 标签处理
					$tags_id_arr = $this->_post('tags_id');
					D('Common')->delete_tags('article_' . $type, 'tags_id', $article_id);
					foreach ($tags_id_arr as $tags_id) {
						D('Common')->add_tags('article_' . $type, 'tags_id', $article_id, $tags_id);
					}

					$this->success('修改成功！', U('index', array('type' =>  $type)));
				}else{
					$this->error('修改失败！');
				}
			}else{
				$this->error($model->getError());
			}
		}
		$where['id'] = $article_id;
		$info = D('Article')->where($where)->find();
		unset($where);
		$where['article_id'] = $article_id;
		$info['content'] = D('ArticleContent')->where($where)->getField('content');

		$this->assign('info', $info);
		$this->display();
	}
	//待审核
	public function audit(){
		$where['status']    = 2;
		$where['is_delete'] = 0;
		parent::index('',$where,'edit_time desc,id desc');
	}
	//待审核
	public function refuse(){
		$where['status']    = 3;
		$where['is_delete'] = 0;
		$this->_use_tpl = 'audit';
		parent::index('',$where,'edit_time desc,id desc');
	}
	//审核通过
	public function audit_pass(){
		$id  = $this->_get('id');
		$ids = $this->_post('ids');
		//使用模型
		$model = D('Article');
		$data['status'] = 1;

		if(!is_array($ids) && is_numeric($id)){
			$where['id'] = $id;
			if($model->where($where)->save($data) == true){
				$this->success('审核通过!');
			}else{
				$this->error('操作失败!');
			}
		}
		elseif(is_array($ids)){
			$ids         = implode(',', $ids);
			$where['id'] = array('in', $ids);
			$model->where($where)->save($data);
			$this->success('批量审核通过成功!');
		}
		else{
			$this->error('请选择要审核记录!');
		}
	}

	//审核拒绝
	public function audit_refuse(){
		$id  = $this->_get('id');
		$ids = $this->_post('ids');
		//使用模型
		$model = D('Article');
		$data['status']         = 3;
		$data['audit_admin_id'] = $this->_user_id;
		$data['audit_time']     = time();

		if(!is_array($ids) && is_numeric($id)){
			$where['id'] = $id;
			if($model->where($where)->save($data) == true){
				$this->success('审核拒绝!');
			}else{
				$this->error('操作失败!');
			}
		}
		elseif(is_array($ids)){
			$ids         = implode(',', $ids);
			$where['id'] = array('in', $ids);
			$model->where($where)->save($data);
			$this->success('批量审核拒绝成功!');
		}
		else{
			$this->error('请选择要审核记录!');
		}
	}
}




